package com.gukun.springboot.tdengine.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gukun.springboot.tdengine.domain.common.CommonResult;
import com.gukun.springboot.tdengine.domain.managetag.DbsManagetagInfo;
import com.gukun.springboot.tdengine.dto.DataSubscriptionDto;
import com.gukun.springboot.tdengine.dto.DsDto;
import com.gukun.springboot.tdengine.dto.ShowTableDto;
import com.gukun.springboot.tdengine.service.datasubscription.DataSubscriptionService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@RequestMapping("/api/tdengine/dataSubscription/")
@RestController
public class DataSubscriptionController {

    @Autowired
    private DataSubscriptionService dataSubscriptionService;

    /**
     * 查询数据库中的主题
     *
     * @return 数据库中的主题
     */
    @Operation(summary = "查询数据库中的主题", description = "查询数据库中的主题")
    @PostMapping("/showTopics")
    public CommonResult<Page<DataSubscriptionDto>> showTopics(@RequestBody ShowTableDto showTableDto) throws SQLException, IOException {
        Page<DataSubscriptionDto> page = dataSubscriptionService.showTopics(showTableDto);
        if(page== null){
            return CommonResult.error(500,"查询出错，请联系管理员");
        }
        return CommonResult.success(page);
    }

    /**
     * 删除主题
     *
     * @return 删除主题
     */
    @Operation(summary = "删除主题", description = "删除主题")
    @PostMapping("/deleteTopics")
    public CommonResult<String> deleteTopics(@RequestBody DsDto params) throws SQLException {
        String result = dataSubscriptionService.deleteTopics(params);
        if(result== null){
            return CommonResult.error(500,"删除出错，请联系管理员");
        }
        return CommonResult.success("删除成功");
    }

    /**
     * 订阅数据同步
     * @param dsDto
     * @return
     * @throws SQLException
     */
    @Operation(summary = "订阅数据同步", description = "订阅数据同步")
    @PostMapping("/syncDataSubInfo")
    public CommonResult<String> syncDataSubInfo(@RequestBody DsDto dsDto) throws Exception {
        String result = dataSubscriptionService.syncDataSubInfo(dsDto);
        if(result== null){
            return CommonResult.error(500,"订阅出错，请联系管理员");
        }
        return CommonResult.success("订阅成功");
    }

    /**
     * 获取结果
     * @return
     * @throws SQLException
     */
    @Operation(summary = "获取结果", description = "获取结果")
    @PostMapping("/getResCount")
    public CommonResult<String> getResCount() throws Exception {
        String result = dataSubscriptionService.getResCount();
        if(result== null){
            return CommonResult.error(500,"订阅出错，请联系管理员");
        }
        return CommonResult.success(result);
    }

    /**
     * 历史数据同步
     * @param dsDto
     * @return
     * @throws SQLException
     */
    @Operation(summary = "历史数据同步", description = "历史数据同步")
    @PostMapping("/syncDataInfo")
    public CommonResult<String> syncDataInfo(@RequestBody DsDto dsDto) throws Exception {
        String result = dataSubscriptionService.syncDataInfo(dsDto);
        if(result== null){
            return CommonResult.error(500,"同步出错，请联系管理员");
        }
        return CommonResult.success("同步成功");
    }

    /**
     * 关闭订阅数据同步
     * @param dsDto
     * @return
     * @throws SQLException
     */
    @Operation(summary = "关闭订阅数据同步", description = "关闭订阅数据同步")
    @PostMapping("/closeSyncDataSubInfo")
    public CommonResult<String> closeSyncDataSubInfo(@RequestBody DsDto dsDto) throws Exception {
        String result = dataSubscriptionService.closeSyncDataSubInfo(dsDto);
        if(result== null){
            return CommonResult.error(500,"关闭订阅出错，请联系管理员");
        }
        return CommonResult.success("关闭订阅成功");
    }



}
